package com.my.test.service.person.dao.impl;

import com.my.test.service.person.dao.PersonDao;
import com.my.test.service.person.model.Person;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by vivi on 2017/11/25.
 */
@Repository("personDao")
public class PersonDaoImpl extends SqlSessionDaoSupport implements PersonDao {

    public void addPerson(Person person) {
        getSqlSession().insert("Person.addPerson",person);
    }

    public void updatePersonByid(Person person) {
        getSqlSession().update("Person.updatePersonByid",person);
    }

    public List<Person> findAllPerson() {
        return getSqlSession().selectList("Person.findAllPerson");
    }

    /**
     * 测试一级缓存，一级缓存是session级别的，查询语句相同，不再去数据库查询，而是从缓存里获取
     * @param id
     * @return
     */
    @Override
    public Person personDetail(String id) {
        SqlSession session = this.getSqlSession();
        Configuration c  = session.getConfiguration();
        session.selectOne("Person.personDetail",id);
        System.out.println("------------------------");
        Person p = session.selectOne("Person.personDetail",id);
        return p;
    }

}
